package cn.com.template.controller.system.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.commons.Paginator;
import cn.com.commons.utils.StringUtil;
import cn.com.dal.dataobject.hr.RoleDO;
import cn.com.dal.dataobject.sys.VerifyConfigDO;
import cn.com.service.sys.RoleService;
import cn.com.service.sys.VerifyConfigService;
import cn.com.support.context.UserContext;
import cn.com.support.message.MessageResult;
import cn.com.template.ui.Flexigrid;
import cn.com.template.utils.FlexigridUtil;

/**
 * 
 * 审核管理控制器
 * @since 1.0 2012-3-31 下午11:03:53
 * @version 1.0
 * @author Zoe
 */
@Controller
@RequestMapping("/system/admin/verifyconfig")
public class VerifyConfigController {

	@Autowired
	private VerifyConfigService configService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping
	public ModelAndView forwardVerifyConfig(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("system/admin/verifyconfig");
		List<RoleDO> roleLst = roleService.selectList(null);
		request.setAttribute("roleLst", roleLst);
		return mav;
	}
	
	@RequestMapping("/getVerifyConfig")
	@ResponseBody
	public Flexigrid getVerifyConfig(HttpServletRequest request){
		Flexigrid flexigrid = null;
		try {
			Paginator<VerifyConfigDO, List<VerifyConfigDO>> paginator = new Paginator<VerifyConfigDO, List<VerifyConfigDO>>();
			VerifyConfigDO configDO = new VerifyConfigDO();
			paginator.setCondition(configDO);
			// 设置分页器
			paginator.setPageSize(Integer.parseInt(request.getParameter("rp")));
			paginator.setCurrentPage(Integer.parseInt(request.getParameter("page")));
			configService.selectByPage(paginator);
			//PACKAGE FLEXIGRID DATA
			flexigrid = FlexigridUtil.formatFlexigridData(paginator);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flexigrid;
	}

	@RequestMapping("/updateVerifyConfig")
	@ResponseBody
	public MessageResult<Integer> updateVerifyConfig(HttpServletRequest request, HttpServletResponse response){
		MessageResult<Integer> result = null;
		try {
			String configName = request.getParameter("configName");
			String description = request.getParameter("description");
			String levelData = request.getParameter("levelData");
			String id = request.getParameter("configId");
			VerifyConfigDO configDO = new VerifyConfigDO();
			configDO.setId(Long.parseLong(id.trim()));
			if(StringUtil.isNotBlank(configName)){
				configDO.setVerifyName(configName.trim());
			}
			configDO.setDescription(description.trim());
			if(StringUtil.isNotBlank(levelData)){
				configDO.setRoleId(levelData);
			}
			configDO.setModifier(UserContext.getUsername());
			result = configService.updateById(configDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
}
